package com.hs.srm.admin.modules.sys.controller;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hs.srm.base.common.utils.AddressDataUtil;
import com.hs.srm.base.modules.sys.entity.SysAreasEntity;
import com.hs.srm.base.modules.sys.entity.SysCitiesEntity;
import com.hs.srm.base.modules.sys.entity.SysCountriesEntity;
import com.hs.srm.base.modules.sys.entity.SysProvincesEntity;
import com.hs.srm.common.utils.R;

/**
 * @author Wangpeidong
 * @create 2018-05-20 13:35
 **/
@Controller
@RequestMapping("/sys/address")
public class SysAddressController extends AbstractController {

	private static final long serialVersionUID = 8459236506623446083L;

	@RequestMapping("/getAllCountry")
	@ResponseBody
	public R getAllCountry() {
		List<SysCountriesEntity> list = AddressDataUtil.getCountries();
		return R.ok().put("countryList", list);
	}

	@RequestMapping("/getAllProvince")
	@ResponseBody
	public R getAllProvince() {
		List<SysProvincesEntity> list = AddressDataUtil.getProvinces();
		return R.ok().put("provinceList", list);
	}

	/**
	 * 根据城市编码获取区县列表
	 * @param code
	 * @return
	 */
	@RequestMapping("/getAreaListByCityCode/{code}")
	@ResponseBody
	public R getAreaListByCityCode(@PathVariable("code") String code) {
		// List<SysAreasEntity> list = this.sysAreasService.selectList(new EntityWrapper<SysAreasEntity>().where(
		// "city_code={0}", code));
		List<SysAreasEntity> list = AddressDataUtil.getAreasByCity(code);
		return R.ok().put("areaList", list);
	}

	/**
	 * 根据省份编码获取城市列表
	 * @param code
	 * @return
	 */
	@RequestMapping("/getCityListByProvinceCode/{code}")
	@ResponseBody
	public R getCityListByProvinceCode(@PathVariable("code") String code) {
		// List<SysCitiesEntity> list = this.sysCitiesService.selectList(new EntityWrapper<SysCitiesEntity>().where(
		// "province_code = {0}", code));
		List<SysCitiesEntity> list = AddressDataUtil.getCitiesByProvince(code);
		return R.ok().put("cityList", list);
	}

	@RequestMapping("/getCodeByName")
	@ResponseBody
	public R getCodeByName(@RequestParam Map<String, String> nameMap) {
		String province = nameMap.get("provinceName");
		String city = nameMap.get("cityName");
		String area = nameMap.get("areaName");
		return R.ok()
				.put("provinceCode", AddressDataUtil.getCodeByName(province, AddressDataUtil.AddressType.PROVINCE))
				.put("cityCode", AddressDataUtil.getCodeByName(city, AddressDataUtil.AddressType.CITY))
				.put("areaCode", AddressDataUtil.getCodeByName(area, AddressDataUtil.AddressType.AREA));
	}

	@RequestMapping("/getNameByCode")
	@ResponseBody
	public R getNameByCode(@RequestParam Map<String, String> codeMap) {
		String provinceCode = codeMap.get("provinceCode");
		String cityCode = codeMap.get("cityCode");
		String areaCode = codeMap.get("areaCode");
		return R.ok()
				.put("provinceName", AddressDataUtil.getNameByCode(provinceCode, AddressDataUtil.AddressType.PROVINCE))
				.put("cityName", AddressDataUtil.getNameByCode(cityCode, AddressDataUtil.AddressType.CITY))
				.put("areaName", AddressDataUtil.getNameByCode(areaCode, AddressDataUtil.AddressType.AREA));

	}
}
